// pages/myReservation/myReservation.js
import Dialog from '@vant/weapp/dialog/dialog';
import {
  reservationList,
  cancelReservation,
  getCampsite,
  getProject,
  getActivity,
} from "../../api/my"
const http = require("../../api/index")
const {
  baseURL
} = require("../../utils/request")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    camp: '',
    oss_url: baseURL,
    userId: wx.getStorageSync('userInfo').userId,
    reservationList: ''
  },
  async getReservationList(id) {
    const res = await reservationList({
      id
    })
    if (res.code === 200) {
      this.setData({
        reservationList: res.rows
      })
      for (let i = 0; i < res.rows.length; i++) {
        this.getCamp(res.rows[i].campId, i)
        for (let j = 0; j < res.rows[i].detailsList.length; j++) {
          if (res.rows[i].detailsList[j].type === "1") {
            this.getCampsiteDil(res.rows[i].detailsList[j].campsiteId,i,j)
          }else if (res.rows[i].detailsList[j].type === "2"){
            this.getProjectDil(res.rows[i].detailsList[j].projectId,i,j)
          }else if (res.rows[i].detailsList[j].type === "3"){
            this.getActivityDil(res.rows[i].detailsList[j].activityId,i,j)
          }
          if (res.rows[i].detailsList[j].number === 0) {
            res.rows[i].detailsList.splice(j, 1)
          }
        }
      }
    }
    console.log(res.rows);
  },
  getCamp(campId, i) {
    var reservation = "reservationList[" + i + "]";
    http.campDetail(campId).then(res => {
      if (res.code == 200) {
        if (res.data.imageUrl.indexOf(',') != -1) {
          res.data.images = res.data.imageUrl.split(',');
          res.data.imageUrl = res.data.images[0];
        }
        this.setData({
          [reservation]: {
            ...this.data.reservationList[i],
            ...res.data
          }
        });
      }
    });
  },

  // 取消预约
  async delReservation(e) {
    console.log(e);
    const id = e.currentTarget.dataset.id
    const res = await cancelReservation(id)
    console.log(res);
    if (res.code === 200) {
      wx.showToast({
        title: '取消成功！',
        icon: 'success',
        duration: 1500
      })
      this.getReservationList()
    }
  },
  async getCampsiteDil(id,i,j) {
    const res = await getCampsite(id)
    console.log(res);
    var detailsList = "reservationList[" + i + "].detailsList[" + j + "]";
    if(res.code === 200){
      this.setData({
        [detailsList]: {
          ...this.data.reservationList[i].detailsList[j],
          ...res.data
        }
      });
    }
  },
  async getProjectDil(id,i,j) {
    const res = await getProject(id)
    var detailsList = "reservationList[" + i + "].detailsList[" + j + "]";
    if(res.code === 200){
      this.setData({
        [detailsList]: {
          ...this.data.reservationList[i].detailsList[j],
          ...res.data
        }
      });
    }
  },
  async getActivityDil(id,i,j) {
    const res = await getActivity(id)
    var detailsList = "reservationList[" + i + "].detailsList[" + j + "]";
    if(res.code === 200){
      this.setData({
        [detailsList]: {
          ...this.data.reservationList[i].detailsList[j],
          ...res.data
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const userId = wx.getStorageSync('userInfo').userId
    this.getReservationList(userId)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})